//
//  EditNameViewController.m
//  ChineseCollegeStudentsNetwork
//
//  Created by 刘喆 on 2018/5/14.
//  Copyright © 2018年 刘喆. All rights reserved.
//

#import "EditNameViewController.h"
#import "CEAccessoryTextField.h"

@interface EditNameViewController ()<UITextFieldDelegate>

/** 修改姓名 */
@property(nonatomic, strong) CEAccessoryTextField *editnameTextField;

@end

@implementation EditNameViewController


#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"修改姓名";
    [self setupConstraints];
    
    UIBarButtonItem *saveItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction)];
    self.navigationItem.rightBarButtonItem = saveItem;

}

- (void)saveAction {
    
    if (self.editnameTextField.text.length == 0) {
        ShowMessage(@"请输入姓名");
        return;
    }
    
    [MLAHttpManage postHttpType:MLAHttpRequestType_Put hostType:MLAHttpHostType_Api api:[self getUserIdApi:@"/api/v1/users"] params:@{@"displayName":self.editnameTextField.text}.mj_JSONString success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        [UserManager sharedInstance].userModel.value.displayName = self.editnameTextField.text;
        ShowMessage(@"修改成功");
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^{
        ShowMessage(@"修改失败,请重新修改");
    }];
}

#pragma mark - Constraints

- (void)setupConstraints {
    
    [self.editnameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(50);
        make.centerX.equalTo(self.view);
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.height.mas_equalTo(@50);
    }];
        
}

- (CEAccessoryTextField *)editnameTextField {
    
    if (!_editnameTextField) {
        _editnameTextField = [[CEAccessoryTextField alloc] init];
        _editnameTextField.type = CEAccessoryTextFieldTypeLabel;
        _editnameTextField.titleLabel.text = @"  姓名：";
        _editnameTextField.placeholder = @"请输入姓名";
        _editnameTextField.text = [UserManager sharedInstance].userModel.value.displayName;
        _editnameTextField.delegate = self;
        _editnameTextField.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_editnameTextField];
    }
    return _editnameTextField;
    
    
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    
    return YES;
}

#pragma mark - UIControl

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self resignAllTextFieldResponder];
}

#pragma mark - Private

- (void)resignAllTextFieldResponder {
    
    [self.editnameTextField resignFirstResponder];

}



@end
